<template>
  <div class="login-wrap">
    <el-form label-position="right"  :model="ruleForm" :rules="rules" ref="ruleForm" label-width="60px" class="demo-ruleForm login-container">
      <h3 class="title">用户登录</h3>
      <el-form-item prop="code" label="工号">
        <el-input type="text" v-model="ruleForm.code" auto-complete="off" placeholder="请输入工号"></el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input type="password" v-model="ruleForm.password" auto-complete="off" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button type="primary" style="width:100%;" @click="submitForm('ruleForm')" :loading="logining">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {Apilogin,ApimenusUser} from '@/api'
import {initDynamicRoutes} from '@/router/index'
export default {
  name: 'login',
  data() {
    return {
      //定义loading默认为false
      logining: false,
      ruleForm: {
        //code和password默认为空
        code: '',
        password: '',
      },
      //rules前端验证
      rules: {
        code: [
          { required: true, message: '请输入工号', trigger: 'blur' },
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
          // { pattern: /[A-Za-z0-9\u4e00-\u9fa5]+$/g, message: '不允许输入空格等特殊符号' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
      }
    }
  },

  methods: {
    getMenuInfo(){
      ApimenusUser().then(result => {
        this.$store.commit('setRightList',result.data )
        this.logining = false
        this.$message.success('登录成功！')
        initDynamicRoutes()
        this.$router.push({ path: '/' })
      }).catch(()=>{
        this.logining = false
      })
    },
    //获取info列表
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.logining = true
          // 测试通道，不为空直接登录
          Apilogin(this.ruleForm).then(res => {
            if(res.code == 200){
              this.$store.commit('setUserInfo',res.data.employee )
              sessionStorage.setItem('token','Bearer ' + res.data.token)
              this.getMenuInfo()
            }
          }).catch(()=>{
            this.logining = false
          })
        } else {
          this.$message.error('请输入工号和密码！')
          this.logining = false
          return false
        }
      })
    },
  }
}
</script>

<style scoped lang='less'>
.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding-top: 10%;
  background-image: url();
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
  
}
.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 30px 35px 15px 15px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}
.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
.remember {
  margin: 0px 0px 35px 0px;
}
.code-box {
  text-align: right;
}
.codeimg {
  height: 40px;
}
/deep/.el-form-item__label::before{
 content: '' !important;
}
</style>